import logging
import sys
from settings import settings


def new_logger(file_name, level=logging.DEBUG, stdout_handler: bool = False):
    formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s", datefmt="%d-%b-%y %H:%M:%S")

    _logger = logging.getLogger(f'{file_name}-logger')
    _logger.setLevel(level)

    fhdlr = logging.FileHandler(f'{settings.ROOT_PATH}/{file_name}.log', mode='a', encoding='utf-8')
    fhdlr.setFormatter(formatter)
    _logger.addHandler(fhdlr)

    if stdout_handler:
        stdout_handler = logging.StreamHandler(sys.stdout)
        stdout_handler.setFormatter(formatter)
        _logger.addHandler(stdout_handler)

    return _logger
